#include "changethingswidget.h"

ChangeThingsWidget::ChangeThingsWidget(QWidget *parent) : QWidget(parent)
{
    init_Widget();
    init_Control();
    init_Connect();
}

void ChangeThingsWidget::init_Control()
{

    this->confirmBtn=new QPushButton();
    this->confirmBtn->setText(QString::fromUtf8("确认"));
    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));
    this->changeReturnBtn=new QPushButton();
    this->changeReturnBtn->setText(QString::fromUtf8("返回"));

    this->confirmCombo=new QComboBox();
    this->confirmCombo->addItem("用户id");
    this->confirmCombo->addItem("物品名");
    this->confirmCombo->addItem("物品类型");
    this->confirmCombo->addItem("物品价格");
    this->confirmCombo->addItem("物品数量");

    this->changeEdit=new QLineEdit();




     this->table=new QTableWidget();
     // 设置表格列数
     this->table->setColumnCount(7); // 增加一列用于操作按钮
     // 设置列标题
     QStringList headers;
     headers << "物品id" << "用户id" << "物品名" << "物品类型" << "物品价格" << "物品数量"<<"操作";
     this->table->setHorizontalHeaderLabels(headers);
     things_show();
     for(int i=0;i<table->rowCount();i++)
     {
         this->changeBtn=new QPushButton();
         this->changeBtn->setText(QString::fromUtf8("修改"));
         table->setCellWidget(i, 6, changeBtn);
         connect(changeBtn, &QPushButton::clicked, [=]() {
             // 调用统一的处理函数，传入行号
             on_changeBtn_cliked(i);
         });
     }

     // 禁用行标题
     this->table->verticalHeader()->setVisible(false);
     // 设置列宽策略 - 让列标题自动占满整个表格宽度
     this->table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
     // 设置表格为不可编辑状态
     this->table->setEditTriggers(QAbstractItemView::NoEditTriggers);
     // 设置表格选择行为 - 选择整行
     this->table->setSelectionBehavior(QAbstractItemView::SelectRows);
     // 设置表格选择模式 - 单选
     this->table->setSelectionMode(QAbstractItemView::SingleSelection);



     this->btnLay=new QHBoxLayout();
     this->btnLay->addWidget(changeReturnBtn);
     this->btnLay->addWidget(confirmBtn);

     this->changeUpLay=new QHBoxLayout();
     this->changeUpLay->addWidget(this->confirmCombo);
     this->changeUpLay->addWidget(this->changeEdit);

     this->otherLay=new QVBoxLayout();
     this->otherLay->addLayout(changeUpLay);
     this->otherLay->addLayout(this->btnLay);


     this->vLay=new QVBoxLayout();
     this->vLay->addWidget(this->table);
     this->vLay->addWidget(this->returnBtn);

     this->setLayout(this->vLay);
}

void ChangeThingsWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("修改用户信息"));
    this->resize(800,600);
}

void ChangeThingsWidget::init_Connect()
{
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
     connect(this->changeReturnBtn,SIGNAL(clicked()),this,SLOT(on_changeReturnBtn_cliked()));
    connect(this->confirmBtn,SIGNAL(clicked()),this,SLOT(on_confirmBtn_cliked()));
    connect(confirmCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_Index_Changed(int)));
}

void ChangeThingsWidget::things_show()
{
    QString sql=QString("SELECT *FROM StockRecord ;");
    int j=0,i=0;
    addstock.resize(100);//初始化
    //adduser.fill(nullptr);
    this->model=this->control->getInstance()->Creatmodel("wuping");
    this->addstock=this->model->getStock((char*)sql.toStdString().c_str());//将员工数据从数据库拉出来
    //qDebug()<<"用户id："<<addstock[0].getUser_id();
    this->table->setRowCount(this->model->getRow((char*)sql.toStdString().c_str()));//设置表格行数
    for(i;i<this->addstock.size();i++)
    {
       j=0;
        //qDebug()<<"运行插入表数据";
       QString user_identity="普通用户";
        //将int型转换成string型
        QString userIdStr = QString::number(this->addstock[i].getUser_id());
        QString tingsIdStr = QString::number(this->addstock[i].getTings_id());
        QString tingsMoney = QString::number(this->addstock[i].getTings_money());
        QString tingscount = QString::number(this->addstock[i].getTings_count());

        //将所有用户信息放入表中
        this->table->setItem(i,j++,new QTableWidgetItem(tingsIdStr));
        this->table->setItem(i,j++,new QTableWidgetItem(userIdStr));
        this->table->setItem(i,j++,new QTableWidgetItem(this->addstock[i].getTings_name()));
        this->table->setItem(i,j++,new QTableWidgetItem(this->addstock[i].getTings_jur()));
        this->table->setItem(i,j++,new QTableWidgetItem(tingsMoney));
        this->table->setItem(i,j++,new QTableWidgetItem(tingscount));


    }
}



void ChangeThingsWidget::on_retutrnBtn_cliked()
{
    emit returnManager();
}

void ChangeThingsWidget::on_changeReturnBtn_cliked()
{
    this->myDialog->close();
}

void ChangeThingsWidget::on_confirmBtn_cliked()
{
    int ture;
    QString Info,id,sql;
    Info=this->changeEdit->text();
    qDebug()<<addstock[row].getTings_id();
    if(this->index==0)
    {
        sql=QString("UPDATE StockRecord SET user_id=%1 WHERE tings_id=%2;").arg(Info).arg(addstock[row].getTings_id());
    }
    else if (this->index==1)
    {
        sql=QString("UPDATE StockRecord SET tings_name='%1' WHERE tings_id=%2;").arg(Info).arg(addstock[row].getTings_id());
    }
    else if (this->index==2)
    {
        sql=QString("UPDATE StockRecord SET tings_jur='%1' WHERE tings_id=%2;").arg(Info).arg(addstock[row].getTings_id());
    }
    else if (this->index==3)
    {
        sql=QString("UPDATE StockRecord SET tings_money=%1 WHERE tings_id=%2;").arg(Info).arg(addstock[row].getTings_id());
    }
    else if (this->index==4)
    {
        sql=QString("UPDATE StockRecord SET tings_count=%1 WHERE tings_id=%2;").arg(Info).arg(addstock[row].getTings_id());
    }
        this->model=this->control->getInstance()->Creatmodel("wuping");
    ture=this->model->Table_Inserter((char*)sql.toStdString().c_str());
    if(ture)
    {
         QMessageBox::information(this,"成功","修改成功");

    }
    else
    {
        QMessageBox::critical(this, "错误", "修改失败");

        this->changeEdit->clear();

    }
}

void ChangeThingsWidget::on_changeBtn_cliked(int row)
{
    //qDebug() << "点击了第" << row << "行的按钮";
    this->row=row;
    myDialog=new QDialog();
    myDialog->setWindowTitle("修改数据");
    myDialog->resize(400,300);
    myDialog->setLayout(this->otherLay);
    myDialog->exec();

    things_show();
    this->myDialog->close();


}

void ChangeThingsWidget::on_Index_Changed(int index)
{
    //查看修改什么
    this->index=index;
}


